package com.chen.demo.javase.juc.aqs;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * AQS 相关功能测试
 * @author hongwei.chen
 * @date 2020/10/03 21:23
 **/
public class AQSTest {
    private static ReentrantLock lock = new ReentrantLock();

    private static CountDownLatch countDownLatch = new CountDownLatch(3);

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo1 thread1 = new ThreadDemo1();
        ThreadDemo1 thread2 = new ThreadDemo1();
        ThreadDemo1 thread3 = new ThreadDemo1();
        thread1.start();
        thread2.start();
        //thread3.start();

        LockSupport.park(3_000_000);
        countDownLatch.await();
    }


    public static class ThreadDemo1 extends Thread {
        @Override
        public void run() {
            lock.lock();
            long statTime = System.currentTimeMillis();
            try {
                System.out.println(Thread.currentThread().getName() + "获取到锁");
                LockSupport.parkNanos(10_000_000_000L);
            } finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName() + "释放锁,"+(System.currentTimeMillis() -statTime));
            }
        }
    }
}
